package com.hanlink.dsp.ca.model.channel;

import com.hanlink.dsp.BtDspApplication;
import com.hanlink.dsp.DspSettings;
import com.hanlink.dsp.util.BLog;
import com.hanlink.dsp.util.ByteUtil;
import com.qztech.bluetooth.BluetoothClientManager;
import com.qztech.utils.YzDebug;

import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;

import java.io.Serializable;
import java.util.Arrays;

/**
 * Created by zhb on 2017/11/29.
 */
@Table(name = "_summing_channel")
public class SummingChannel implements Serializable {
    @Column(name = "id", isId = true)
    protected int id;

    @Column(name = "channel")
    protected int channel;

    @Column(name = "settings_id")
    private int settingsId;

    private int[] high_input_channels = new int[DspSettings.CHANNEL_LENGTH];
    private int[] low_input_channels = new int[DspSettings.CHANNEL_LENGTH];

    @Column(name = "high_input_val")
    private String highInputVal = Arrays.toString(high_input_channels);
    @Column(name = "low_input_val")
    private String lowInputVal = Arrays.toString(low_input_channels);
    @Column(name = "is_selected")
    private boolean isSelected;

    public SummingChannel() { }



    public void resetData() {
        isSelected = false;
        Arrays.fill(high_input_channels, 0);
        Arrays.fill(low_input_channels, 0);
        highInputVal = Arrays.toString(high_input_channels);
        lowInputVal = Arrays.toString(low_input_channels);
    }
    public SummingChannel(int channel, int[] high_input_channels, int[] low_input_channels) {
        this.channel = channel;
        this.high_input_channels = high_input_channels;
        this.low_input_channels = low_input_channels;
        highInputVal = Arrays.toString(high_input_channels);
        lowInputVal = Arrays.toString(low_input_channels);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getChannel() {
        return channel;
    }

    public void setChannel(int channel) {
        this.channel = channel;
    }

    public int getSettingsId() {
        return settingsId;
    }

    public void setSettingsId(int settingsId) {
        this.settingsId = settingsId;
    }

    public int[] getHigh_input_channels() {
        return high_input_channels;
    }

    public void setHigh_input_channels(int[] high_input_channels) {
        this.high_input_channels = high_input_channels;
    }
    public void resetHighInputChannels(boolean isSave){
        Arrays.fill(high_input_channels, 0);
        if (isSave)
            highInputVal = Arrays.toString(this.high_input_channels);
    }

    public String getHighInputVal() {
        return highInputVal;
    }

    public void setHighInputVal(String highInputVal) {
        this.highInputVal = highInputVal;
        if (highInputVal == null || highInputVal.length() <= 0) return;
        String[] tempInput = highInputVal.replace("[", "").replace("]", "").split(",");
        try {
            for (int i = 0; i < tempInput.length; i++) {
                this.high_input_channels[i] = Integer.parseInt(tempInput[i].trim());
            }
        } catch (Exception ex) {
            System.out.println(" setHighInputVal ERROR:" + ex);
        }
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }

    public void setHighInputChannel(int index, boolean isChecked) {
        if (this.high_input_channels == null || this.high_input_channels.length <= 0) {
            this.high_input_channels = new int[DspSettings.CHANNEL_LENGTH];
        }
        int newValue = isChecked ? 1 : 0;

        if (this.high_input_channels[index] == newValue) {
            return;
        }
        this.high_input_channels[index] = newValue;
        highInputVal = Arrays.toString(this.high_input_channels);
        writeCmd(getCmd(true));
    }

    public void setHighInputChannelNoSendCmd(int index, boolean isChecked) {
        if (this.high_input_channels == null || this.high_input_channels.length <= 0) {
            this.high_input_channels = new int[DspSettings.CHANNEL_LENGTH];
        }
        int newValue = isChecked ? 1 : 0;

        if (this.high_input_channels[index] == newValue) {
            return;
        }
        this.high_input_channels[index] = newValue;
        highInputVal = Arrays.toString(this.high_input_channels);
    }

    public int getLowInputChannel(int index){
        if (index >= 0 && index < low_input_channels.length){
            return low_input_channels[index];
        }
        return 0;
    }
    public void setLowInputChannel(int index, int value) {
        if (this.low_input_channels == null || this.low_input_channels.length <= 0) {
            this.low_input_channels = new int[DspSettings.CHANNEL_LENGTH];
        }

        if (this.low_input_channels[index] == value) {
            return;
        }
        this.low_input_channels[index] = value;
        lowInputVal = Arrays.toString(this.low_input_channels);
//        writeCmd(getCmd(false));
    }
    public void setLowInputChannel(int index, boolean isChecked) {
        if (this.low_input_channels == null || this.low_input_channels.length <= 0) {
            this.low_input_channels = new int[DspSettings.CHANNEL_LENGTH];
        }
        int newValue = isChecked ? 1 : 0;

        if (this.low_input_channels[index] == newValue) {
            return;
        }
        this.low_input_channels[index] = newValue;
        lowInputVal = Arrays.toString(this.low_input_channels);
        writeCmd(getCmd(false));
    }

    public int[] getLow_input_channels() {
        return low_input_channels;
    }

    public void setLow_input_channels(int[] low_input_channels) {
        this.low_input_channels = low_input_channels;
    }

    public String getLowInputVal() {
        return lowInputVal;
    }

    public void setLowInputVal(String lowInputVal) {
        this.lowInputVal = lowInputVal;
        if (lowInputVal == null || lowInputVal.length() <= 0) return;
        String[] tempInput = lowInputVal.replace("[", "").replace("]", "").split(",");
        try {
            for (int i = 0; i < tempInput.length; i++) {
                this.low_input_channels[i] = Integer.parseInt(tempInput[i].trim());
            }
        } catch (Exception ex) {
            System.out.println(" setLowInputVal ERROR:" + ex);
        }
    }

    public byte[] getCmd(boolean isHigh) {
        byte[] EQ_CHANNEL = new byte[]{(byte) 0xAA, (byte) 0x55, (byte) 0xAA, (byte) 0x83, (byte) 0xF0, (byte) 0xC0, 0x00, 0x00, 0x00, 0x00, (byte) 0xff, 0x0F, 0x7c, 0x55};
        EQ_CHANNEL[5] = (byte) (0xC1 + channel);
        int[] input = high_input_channels;
        int i = 6;
        for (int value : input) {
            EQ_CHANNEL[i++] = (byte) value;
        }
//        EQ_CHANNEL[i++] = (byte) input[0];
//        EQ_CHANNEL[i++] = (byte) input[1];
//        EQ_CHANNEL[i++] = (byte) input[2];
//        EQ_CHANNEL[i++] = (byte) input[3];
//        EQ_CHANNEL[i++] = (byte) input[4];
//        EQ_CHANNEL[i++] = (byte) input[5];
//        EQ_CHANNEL[i++] = (byte) input[6];
//        EQ_CHANNEL[i++] = (byte) input[7];
        return EQ_CHANNEL;
    }
    public void writeCmd(){
        writeCmd(getCmd(true));
    }
    public void writeCmd(byte[] cmd) {
        BLog.d("summing", ByteUtil.bytesToHexString(cmd));
        BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(cmd);
    }


    public SummingChannel copy() {
        final SummingChannel ss = new SummingChannel();
        ss.id = id;
        ss.channel = channel;
        ss.settingsId = settingsId;
        System.arraycopy(high_input_channels,0,ss.high_input_channels,0,DspSettings.CHANNEL_LENGTH);
        System.arraycopy(low_input_channels,0,ss.low_input_channels,0,DspSettings.CHANNEL_LENGTH);
        ss.highInputVal = highInputVal;
        ss.lowInputVal = lowInputVal;
        ss.isSelected = isSelected;
        return ss;
    }
}
